package edu.njucm.herbiv

import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.*
import org.junit.jupiter.api.Test
import org.springframework.util.DigestUtils
import org.springframework.util.ResourceUtils
import java.io.File
import java.sql.Timestamp
import kotlin.coroutines.*

/**
 * @Author ZhouHang
 * @Date 24/5/17 周五
 */
class LearnTest {
    @Test
    fun `Learn Join`() {
        val list = mutableListOf("12", "abc")
        val code = list.joinToString(separator = ",")
        val now = Timestamp(System.currentTimeMillis())
        val analysisNo = DigestUtils.md5DigestAsHex("$code$now".toByteArray())
        println(analysisNo)
    }

    @OptIn(DelicateCoroutinesApi::class)
    @Test
    fun `Learn Coroutine`() {
        GlobalScope.launch {
            val arg1 = suspendF1()
            val arg2 = suspendF2()
            println("suspend finish arg1:$arg1  arg2:$arg2  result:${arg1 + arg2}")
        }
    }

    private suspend fun suspendF1(): Int {
        delay(1000)
        println("suspend fun 1")
        return 2
    }

    private suspend fun suspendF2(): Int {
        delay(1000)
        println("suspend fun 2")
        return 4
    }

    @Test
    fun `Learn Coroutine2`() {
        val continuation = suspend {
            println("In Coroutine")
            5
        }.createCoroutine(object : Continuation<Int> {
            override fun resumeWith(result: Result<Int>) {
                println("Coroutine result:$result")
            }

            override val context = EmptyCoroutineContext
        })
        continuation.resume(Unit)
    }

    @Test
    fun `Learn Coroutine3`() {
        testRunBlocking()
    }

    private fun testRunBlocking() = runBlocking { // this: CoroutineScope
        launch { // launch a new coroutine and continue
            delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
            println("World!") // print after delay
        }
        println("Hello") // main coroutine continues while a previous one is delayed
    }

    @Test
    fun `Learn Sort`() {
        val a = listOf("r", "c", "a", "t", "b", "d")
        val b = listOf("w", "q")
        val c = a + b
        val d = c.sortedBy { it }
        c.forEach(::println)
        d.forEach(::println)
    }

    @Test
    fun `Write Content`() {
        val path = "N:\\Code\\WebApplication\\HerbiV-Web\\data\\a.txt"
        File(path).writeText("123123")
    }

    @Test fun `Test getEnv`() {
        val herbivHOME = System.getenv("HERBIV_HOE")
        println(herbivHOME)
    }
}